package com.xx.library.service.impl;

import com.xx.library.entity.Book;
import com.xx.library.entity.BorrowRecord;
import com.xx.library.mapper.BorrowRecordMapper;
import com.xx.library.service.BookService;
import com.xx.library.service.BorrowRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class BorrowRecordServiceImpl implements BorrowRecordService {

    @Autowired
    private BorrowRecordMapper borrowRecordMapper;

    @Autowired
    private BookService bookService;

    @Override
    public BorrowRecord getBorrowRecordById(Long id) {
        return borrowRecordMapper.selectById(id);
    }

    @Override
    public List<BorrowRecord> getBorrowRecordsByUserId(Long userId) {
        return borrowRecordMapper.selectByUserId(userId);
    }

    @Override
    public List<BorrowRecord> getBorrowRecordsByBookId(Long bookId) {
        return borrowRecordMapper.selectByBookId(bookId);
    }

    @Override
    public List<BorrowRecord> getCurrentBorrows(Long userId) {
        return borrowRecordMapper.selectCurrentBorrows(userId);
    }

    @Override
    public List<BorrowRecord> getOverdueRecords() {
        return borrowRecordMapper.selectOverdueRecords();
    }

    @Override
    public List<BorrowRecord> getBorrowRecordsByCondition(BorrowRecord borrowRecord) {
        return borrowRecordMapper.selectByCondition(borrowRecord);
    }

    @Override
    @Transactional
    public void borrowBook(Long userId, Long bookId) {
        // 检查书籍是否存在且可借
        Book book = bookService.getBookById(bookId);
        if (book == null) {
            throw new RuntimeException("书籍不存在");
        }
        if (book.getAvailableCopies() <= 0) {
            throw new RuntimeException("书籍已无可借数量");
        }

        // 检查用户是否已借阅此书
        List<BorrowRecord> currentBorrows = getCurrentBorrows(userId);
        for (BorrowRecord record : currentBorrows) {
            if (record.getBookId().equals(bookId)) {
                throw new RuntimeException("您已借阅此书");
            }
        }

        // 创建借阅记录
        BorrowRecord borrowRecord = new BorrowRecord();
        borrowRecord.setUserId(userId);
        borrowRecord.setBookId(bookId);
        borrowRecord.setBorrowDate(LocalDateTime.now());
        borrowRecord.setDueDate(LocalDateTime.now().plusDays(30)); // 默认借阅30天
        borrowRecord.setStatus(1); // 1表示借阅中

        // 更新书籍可借数量
        bookService.updateAvailableCopies(bookId, book.getAvailableCopies() - 1);

        // 保存借阅记录
        borrowRecordMapper.insert(borrowRecord);
    }

    @Override
    @Transactional
    public void returnBook(Long id) {
        BorrowRecord record = getBorrowRecordById(id);
        if (record == null) {
            throw new RuntimeException("借阅记录不存在");
        }
        if (record.getStatus() != 1) {
            throw new RuntimeException("该记录不是借阅中状态");
        }

        // 更新借阅记录
        record.setReturnDate(LocalDateTime.now());
        record.setStatus(0); // 0表示已归还
        borrowRecordMapper.update(record);

        // 更新书籍可借数量
        Book book = bookService.getBookById(record.getBookId());
        bookService.updateAvailableCopies(book.getId(), book.getAvailableCopies() + 1);
    }

    @Override
    @Transactional
    public void renewBook(Long id) {
        BorrowRecord record = getBorrowRecordById(id);
        if (record == null) {
            throw new RuntimeException("借阅记录不存在");
        }
        if (record.getStatus() != 1) {
            throw new RuntimeException("该记录不是借阅中状态");
        }

        // 更新借阅记录
        record.setDueDate(record.getDueDate().plusDays(30)); // 续借30天
        borrowRecordMapper.update(record);
    }
} 